import DialogStore from ".";
import { observable, action,toJS } from "mobx";
import { Button,message } from 'antd';
import { each } from 'lodash';
import {WeaLocaleProvider} from "ecCom";
import API from '../../apis/cuspath';
const getLabel = WeaLocaleProvider.getLabel;

export default class OperateMenuDialogStore extends DialogStore {
    
    @observable nodeid ;
    @observable tableDatas = [] ;
    @observable pathStore;
    @observable readonly=false;

    constructor(pathStore) {
        super();
        // this.title = getLabel('510981','节点操作菜单设置')
        this.style = { width: 520, height: 361 };
        this.pathStore = pathStore;
        this.hasScroll = true;
        message.config({top: 60});
    }


    @action open = (params) => {
        console.log("params",params)
        this.params = {};
        each(params, (value, key) => {
            if (value) {
                this.params[key] = value;
            }
        });
        this.readonly = this.pathStore.readonly
        this.title = getLabel('510981','节点操作菜单设置');
        this.buttons = this.getButtons();
        this.nodeid=this.params.nodeid;
        this.visible = true;
        this.loading = true;
        API.getOperateMenu(this.params).then(result=>{
            this.loading = false;
            this.tableDatas = result;
        });
    }

    @action save = () => {
        const isUsedString  = this.tableDatas.map(d=>d.isUsed).join(",")
        API.setOperateMenu({nodeid:this.nodeid,isUsedStr:isUsedString}).then(()=>{
             message.success(getLabel('502231','保存成功'));
             this.hide();
        })
    }
    
    @action setTableDatas = (datas) => {
        this.tableDatas = datas;
    }

    @action getButtons = () => {
        const buttons = [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@7jgdoz@cancel`} key="cancel" type="primary" onClick={this.hide}>{getLabel(31129,'取消')}</Button>]
        if(!this.readonly) buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bhcjrb@save`} key="save" type="primary" onClick={this.save}>{getLabel(30986,'保存')}</Button>);
        return buttons;
   }
}
